package com.zwb.test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class PushBoxGameWithGUI extends JPanel {
    private static final int CELL_SIZE = 50;
    private static final int GRID_WIDTH = 8;
    private static final int GRID_HEIGHT = 5;
    private char[][] board;
    private int playerRow;
    private int playerCol;

    public PushBoxGameWithGUI() {
        setPreferredSize(new Dimension(CELL_SIZE * GRID_WIDTH, CELL_SIZE * GRID_HEIGHT));
        initializeBoard();
        addKeyListener(new GameKeyListener());
        setFocusable(true);
    }

    private void initializeBoard() {
        board = new char[][]{
                {'#', '#', '#', '#', '#', '#', '#', '#'},
                {'#', '.', '.', ' ', ' ', ' ', ' ', '#'},
                {'#', '.', '.', ' ', ' ', '$', ' ', '#'},
                {'#', '@', ' ', ' ', ' ', ' ', ' ', '#'},
                {'#', '#', '#', '#', '#', '#', '#', '#'}
        };
        findPlayerPosition();
    }

    // 其他方法与之前提供的相同
    private void findPlayerPosition() {
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if (board[i][j] == '@') {
                    playerRow = i;
                    playerCol = j;
                    return;
                }
            }
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                switch (board[i][j]) {
                    case '#':
                        g.setColor(Color.BLACK);
                        break;
                    case '.':
                        g.setColor(Color.GREEN);
                        break;
                    case '@':
                        g.setColor(Color.BLUE);
                        break;
                    case '$':
                        g.setColor(Color.RED);
                        break;
                    default:
                        g.setColor(Color.WHITE);
                        break;
                }
                g.fillRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
            }
        }
    }

    private class GameKeyListener implements KeyListener {
        @Override
        public void keyTyped(KeyEvent e) {}

        @Override
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            switch (keyCode) {
                case KeyEvent.VK_UP:
                    move(-1, 0);
                    break;
                case KeyEvent.VK_DOWN:
                    move(1, 0);
                    break;
                case KeyEvent.VK_LEFT:
                    move(0, -1);
                    break;
                case KeyEvent.VK_RIGHT:
                    move(0, 1);
                    break;
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {}
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Push Box Game");
            PushBoxGameWithGUI gamePanel = new PushBoxGameWithGUI();

            frame.add(gamePanel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            JOptionPane.showMessageDialog(frame, "Welcome to Push Box Game! Use arrow keys to move the player.");
        });
    }
}
